<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

use yii\helpers\Json;

/* ===================== 动态控件项值校验脚本 ===================== */

/** @var \yii\web\View $this View实例 */
/* @var array $formDynamicControl 表单动态控件选项 */
?>
<script>
    /** @var formData 表单数据 */
    /** @var __isPass 表单数据 */
    /** @var topToastr toastr插件 */

    var __currentGroupData;
    var __currentDynamicControlData;
    var __currentJsItems;

    <?php foreach ($formDynamicControl as $index => $item): ?>

    if (__isPass && formData.hasOwnProperty("<?= $item['__groupField'] ?>")) {
        __currentGroupData = formData["<?= $item['__groupField'] ?>"];
        __currentDynamicControlData = __currentGroupData["<?= $item['__field'] ?>"];
        __currentJsItems = <?= Json::encode($item['__jsItem']) ?>;

        Loop<?= $index + 1 ?>:for (var key in __currentDynamicControlData) {
            var value = __currentDynamicControlData[key];
            for (var field in value) {
                if (
                    __currentJsItems[field]
                    && __currentJsItems[field]['required']
                    && ym2.isEmpty(value[field])
                ) {
                    topToastr.warning('"<?= $item['label'] ?>": 第' + (Number(key) + 1) + '行"' + (__currentJsItems[field]['label'] || field) + '"不可为空!');
                    __isPass = false;
                    break Loop<?= $index + 1 ?>;
                }
            }
        }
    }
    <?php endforeach; ?>

</script>
